package com.photoeditor.demo.ui.widget.image;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.ui.widget.ripple.RippleRelativeLayout;
import com.photoeditor.R;
import com.photoeditor.demo.util.FontUtil;

/**
 * Created by Administrator on 2017/9/23 0023.
 * 背景选中的tab
 */

public class CustomTabCheckBg extends RippleRelativeLayout {

    private boolean mChecked = false;

    private Drawable mBgDrawable;
    private Drawable mBgCheckedDrawable;

    private int mTextColor;

    private ImageView mImageView;
    private TextView mTextView;

    public CustomTabCheckBg(Context context) {
        super(context);
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.layout_custom_tab_check_bg, this, true);
        mImageView = (ImageView) findViewById(R.id.image);
        mTextView = (TextView) findViewById(R.id.text);
        FontUtil.setCustomFont(mTextView);
    }

    public CustomTabCheckBg(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeWithAttrs(attrs);
    }

    private void initializeWithAttrs(AttributeSet attrs) {
        if (attrs != null) {

            LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            li.inflate(R.layout.layout_custom_tab_check_bg, this, true);
            mImageView = (ImageView) findViewById(R.id.image);
            mTextView = (TextView) findViewById(R.id.text);
            FontUtil.setCustomFont(mTextView);

            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTabCheckBg);

            int srcResId = typedArray.getResourceId(R.styleable.CustomTabCheckBg_tabSrc, View.NO_ID);
            if (srcResId != View.NO_ID) {
                mImageView.setImageResource(srcResId);
            }

            String text = typedArray.getString(R.styleable.CustomTabCheckBg_checkTabText);
            if (text != null) {
                mTextView.setText(text);
            }

            int bgSrcId = typedArray.getResourceId(R.styleable.CustomTabCheckBg_tabBg, View.NO_ID);
            int bgSrcSelectedId = typedArray.getResourceId(R.styleable.CustomTabCheckBg_tabBgSelected, View.NO_ID);
            if (bgSrcId == View.NO_ID) {
                int color = typedArray.getColor(R.styleable.CustomTabCheckBg_tabBg, Color.TRANSPARENT);
                if (color != Color.TRANSPARENT) {
                    mBgDrawable = new ColorDrawable(color);
                }
            } else {
                mBgDrawable = getResources().getDrawable(bgSrcId);
            }

            if (bgSrcSelectedId == View.NO_ID) {
                int color = typedArray.getColor(R.styleable.CustomTabCheckBg_tabBgSelected, Color.TRANSPARENT);
                if (color != Color.TRANSPARENT) {
                    mBgCheckedDrawable = new ColorDrawable(color);
                }
            } else {
                mBgCheckedDrawable = getResources().getDrawable(bgSrcSelectedId);
            }

            mChecked = typedArray.getBoolean(R.styleable.CustomTabCheckBg_tabSelected, false);
            if (mChecked) {
                if (mBgCheckedDrawable != null) {
                    setBackgroundDrawable(mBgCheckedDrawable);
                }
            } else {
                if (mBgDrawable != null) {
                    setBackgroundDrawable(mBgDrawable);
                }
            }
            typedArray.recycle();
        }
    }

    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            if (mChecked) {
                if (mBgCheckedDrawable != null) {
                    setBackgroundDrawable(mBgCheckedDrawable);
                }
            } else {
                if (mBgDrawable != null) {
                    setBackgroundDrawable(mBgDrawable);
                }
            }
        }
    }

    public void setText(String text) {
        mTextView.setText(text);
    }

    public void setImageView(int resId) {
        mImageView.setImageResource(resId);
    }

    public boolean isChecked() {
        return mChecked;
    }
}
